using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FetCo.Data;
using ADESSA.Business;
using ADESSA.Entities;
using FetCo.Framework;
using FetCo.Web;
using System.IO;


public partial class miniweb_Contact : System.Web.UI.Page
{
    protected long Id
    {
        get
        {
            object o = ViewState["Id"];
            if (o != null)
                return (long)o;
            return long.MinValue;
        }
        set
        {
            ViewState["Id"] = value;
        }
    }
    MemberCollection _MemCollectionEmail = new MemberCollection();
    MemberCollection _MemCollectionID = new MemberCollection();
    protected string nameCompany = string.Empty;
    protected string emailCompany = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        HtmlAnchor aMenuContact = (HtmlAnchor)Page.Master.FindControl("aMenuContact");
        aMenuContact.Attributes.Add("class", "Select");
        if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            this.Id = long.Parse(Request.QueryString["id"]);
        if (IsPostBack)
            return;
        LoadData();
    }
    #region Load Data Company
    private void LoadData()
    {



        if (Session["CompanyID"] != null)
        {
            this.Id = long.Parse(Session["CompanyID"].ToString());
            Company _Company = CompanyService.GetCompany(this.Id);
            if (_Company != null)
            {
                Session["MemberID"] = _Company.MemberID;
                nameCompany = _Company.CompanyName;
                emailCompany = _Company.Email;

                //Basic Information
                ltrCOntactName.Text = _Company.ContactPerson;
                ltrCompanyName.Text = _Company.CompanyName;
                ltrCOntactName.Text = _Company.ContactPerson;
                ltrTel.Text = _Company.Phone;
                ltrZip.Text = _Company.ZipCode;
                ltrFax.Text = _Company.Fax;
                ltrAddress.Text = _Company.Address;
                ltrWebsite.Text = _Company.Web;
                Country _Country = CountryService.GetCountry(_Company.Country);
                if (_Country != null)
                    ltrCountry.Text = _Country.Name;



            }
        }
    }
    #endregion
    #region Send Contact
    #region Get Email Send To Admin
    private string EmailSend()
    {


        Criteria criuser = new Criteria();
        ADESSA.Entities.UserCollection _UserCollection = UserService.GetUserListByParam(UserColumns.UserId, OrderDirection.ASC, criuser.Criter);
        User _User = new User();
        if (_UserCollection.Count > 0)
        {
            _User = _UserCollection[0];
            return _User.Email.ToString();
        }
        else
            return RTISettings.EmailAdministrator;

    }
    #endregion
    #region Send Mail
    protected void SendMail(string email, string namecompany, string subject, string content)
    {
        Criteria _cri = new Criteria();
        _cri.add("[" + NewsletterColumns.Order + "]", FetConstant.EmailSendContactForCompany, Condition.EQUAL, Condition.AND);
        NewsletterCollection _NewsletterCollection = NewsletterService.GetNewsletterListByParam("[" + Newsletter._Table + "].[" + NewsletterColumns.CreatedDate + "]", OrderDirection.ASC, _cri.Criter);
        if (_NewsletterCollection != null)
        {
            if (_NewsletterCollection.Count > 0)
            {


                string mailContent = string.Format(_NewsletterCollection[0].Content, namecompany,subject, content);
                if (!Utility.SendMailDisplay(email, subject, mailContent, FetConstant.DisplayNameEmail, txtEmailFrom.Value))
                {
                    Alert.Show("Send email for company unsussecc");
                    return;
                }
                if (!Utility.SendMailDisplay(txtEmailFrom.Value, subject, mailContent, FetConstant.DisplayNameEmail, email))
                {
                    Alert.Show("Send email for company unsussecc");
                    return;
                }
                txtSubject.Value = string.Empty;
                txtEmailFrom.Value = string.Empty;
                johndoe.Value = string.Empty;
                Alert.Show("Send email for company sussecc");
            }
        }



    }

    #endregion

    #endregion
    protected void btnSend_Click(object sender, EventArgs e)
    {
        if (Session["CompanyID"] != null)
        {
            this.Id = long.Parse(Session["CompanyID"].ToString());
            Company _Company = CompanyService.GetCompany(this.Id);
            if (_Company != null)
            {

                nameCompany = _Company.CompanyName;
                emailCompany = _Company.Email;
                SendMail(emailCompany, nameCompany, txtSubject.Value, johndoe.Value);
            }
        }
    }
}
